package com.dataseek.iot.toolikit.common.aop;


import com.alibaba.fastjson.JSONObject;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.exception.UnAuthorizedException;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.HttpClientErrorException;

/**
 * @Author: lidong
 * @Description  全局异常处理
 * @Date Created in 2019/1/9 14:12
 */


@Slf4j
@ControllerAdvice(basePackages = "com.dataseek.iot")
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity exceptionHandler(Exception e){
        log.error("【系统抛出Exception异常】 —— 异常内容如下：{}" , e);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg",e.getMessage());
        return new ResponseEntity(jsonObject, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResponseEntity defaultException(RuntimeException e){
        log.error("【系统抛出运行Exception异常】 —— 异常内容如下：{}" , e);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg",e.getMessage());
        return new ResponseEntity(jsonObject, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(UnAuthorizedException.class)
    @ResponseBody
    public ResponseEntity unAuthorizedException(RuntimeException e){
        log.error("【系统抛出运行Exception异常】 —— 异常内容如下：{}" , e);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg",e.getMessage());
        return new ResponseEntity(jsonObject, HttpStatus.UNAUTHORIZED);
    }

}
